<?php

class Silk_Mapi_Adminhtml_VersionController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        $this->_titles = array("轻松老板", 'APP版本管理');
        $this->loadLayout();
        $this->_setActiveMenu('system');
        $this->_addContent($this->getLayout()->createBlock('mapi/adminhtml_version'));
        $this->renderLayout();
    }

    public function newAction()
    {
        $this->_titles = array("轻松老板", 'APP版本管理', '新增');
        $this->loadLayout();
        $this->_setActiveMenu("system");

        $this->getLayout()->getBlock("head")->setCanLoadExtJs(true);

        $this->_addContent($this->getLayout()->createBlock("mapi/adminhtml_version_edit"))->_addLeft($this->getLayout()->createBlock("mapi/adminhtml_version_edit_tabs"));

        $this->renderLayout();
    }

    public function editAction()
    {
        $this->_titles = array("轻松老板", 'APP版本管理', '编辑');

        $id = $this->getRequest()->getParam("id");
        $model = Mage::getModel("mapi/version")->load($id);
        if ($model->getId()) {
            Mage::getSingleton("adminhtml/session")->setVersionData($model->getData());
            $this->loadLayout();
            $this->_setActiveMenu("system");
            $this->getLayout()->getBlock("head")->setCanLoadExtJs(true);
            $this->_addContent($this->getLayout()->createBlock("mapi/adminhtml_version_edit"))->_addLeft($this->getLayout()->createBlock("mapi/adminhtml_version_edit_tabs"));
            $this->renderLayout();
        } else {
            Mage::getSingleton("adminhtml/session")->addError(Mage::helper("mapi")->__("版本信息不存在."));
            $this->_redirect("*/*/");
        }
    }

    public function saveAction()
    {
        $post = $this->getRequest()->getPost();
        if ($post) {
            try {
                $model = Mage::getModel('mapi/version');
                $model->addData($post)
                    ->setId($this->getRequest()->getParam("id"));
                if (isset($post['website_ids'])) {
                    $websiteIds = implode(',', $post['website_ids']);
                    $model->setWebsiteIds($websiteIds);
                }

                $model->save();
                Mage::getSingleton("adminhtml/session")->addSuccess(Mage::helper("adminhtml")->__("已保存！"));
                Mage::getSingleton("adminhtml/session")->setVersionData(false);
                if ($this->getRequest()->getParam("back")) {
                    $this->_redirect("*/*/edit", array("id" => $model->getId()));
                    return;
                }
            } catch (Exception $e) {
                Mage::getSingleton("adminhtml/session")->addError($e->getMessage());
                Mage::getSingleton("adminhtml/session")->setVersionData($this->getRequest()->getPost());
                $this->_redirect("*/*/edit", array("id" => $this->getRequest()->getParam("id")));
            }
        }
        $this->_redirect("*/*/");
    }

    public function deleteAction()
    {
        if ($this->getRequest()->getParam("id") > 0) {
            try {
                $model = Mage::getModel("mapi/version");
                $model->setId($this->getRequest()->getParam("id"))->delete();
                Mage::getSingleton("adminhtml/session")->addSuccess(Mage::helper("adminhtml")->__("已删除！"));
                $this->_redirect("*/*/");
            } catch (Exception $e) {
                Mage::getSingleton("adminhtml/session")->addError($e->getMessage());
                $this->_redirect("*/*/edit", array("id" => $this->getRequest()->getParam("id")));
            }
        }
        $this->_redirect("*/*/");
    }

    public function massRemoveAction()
    {
        try {
            $ids = $this->getRequest()->getPost('ids', array());
            foreach ($ids as $id) {
                $model = Mage::getModel("mapi/version");
                $model->setId($id)->delete();
            }
            Mage::getSingleton("adminhtml/session")->addSuccess(Mage::helper("adminhtml")->__("已删除！"));
        } catch (Exception $e) {
            Mage::getSingleton("adminhtml/session")->addError($e->getMessage());
        }
        $this->_redirect('*/*/');
    }
}